﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmlSchemaProvider.Web.Entities;

namespace XmlSchemaProvider.Web
{
    public static class SchemaExtensions
    {
        public static string GetLocation(this ISchemaParent node)
        {
            StringBuilder sb = new StringBuilder();

            while (node != null && !(node is Schema))
            {
                if (node.Name.Length > 0)
                {
                    sb.Insert(0, "/" + node.Name);
                }

                if (node.Parent is ISchemaParent)
                {
                    node = (ISchemaParent)node.Parent;
                }
                else
                {
                    break;
                }
            }

            return sb.ToString();
        }

        public static string GetLocation(this SchemaAttribute attribute)
        {
            StringBuilder sb = new StringBuilder();
            ISchemaParent node = (ISchemaParent) attribute.Parent;

            sb.Insert(0, "[@" + attribute.Name + "]");

            while (node != null && !(node is Schema))
            {
                if (node.Name.Length > 0)
                {
                    sb.Insert(0, "/" + node.Name);
                }

                if (node.Parent is ISchemaParent)
                {
                    node = (ISchemaParent)node.Parent;
                }
                else
                {
                    break;
                }
            }

            return sb.ToString();
        }

    }
}
